package etc

import (
	"gorm.io/gorm/schema"
	"strings"
)

// GormConfig GORM配置
type GormConfig struct {
	SkipDefaultTransaction                   bool `yaml:"skip_default_transaction,omitempty"`
	DryRun                                   bool `yaml:"dry_run,omitempty"`
	PrepareStmt                              bool `yaml:"prepare_stmt,omitempty"`
	DisableNestedTransaction                 bool `yaml:"disable_nested_transaction,omitempty"`
	AllowGlobalUpdate                        bool `yaml:"allow_global_update,omitempty"`
	DisableAutomaticPing                     bool `yaml:"disable_automatic_ping,omitempty"`
	DisableForeignKeyConstraintWhenMigrating bool `yaml:"disable_foreign_key_constraint_when_migrating,omitempty"`

	NamingStrategy schema.NamingStrategy `yaml:",omitempty"`

	MaxOpenConns int `yaml:"max_open_conns"`
	MaxIdleConns int `yaml:"max_idle_conns"`
}

// DbConnection mysql 驱动配置
type DbConnection struct {
	Name              string `yaml:"name"`
	Drive             string `yaml:"drive"`
	DSN               string `yaml:"dsn"`
	DefaultStringSize uint   `yaml:"default_string_size"`
	Enable            bool   `yaml:"enable"`
}

func initGormConfig() {
	// 设置数据库Gorm命名策略
	Configure.Db.Gorm.NamingStrategy = schema.NamingStrategy{
		TablePrefix:   "",                                // 表名前缀
		SingularTable: true,                              // 使用单数表名，启用该选项后，`User` 表将是`user`
		NameReplacer:  strings.NewReplacer("CID", "Cid"), // 在转为数据库名称之前，使用NameReplacer更改结构/字段名称。
	}
}
